﻿#include "compresssend.h"
#include <QDebug>
#include <QDateTime>
#include <QPixmap>
#include <QtEndian>
#include "command_define.h"

int CompressSend::picIndex=0;

CompressSend::CompressSend(QImage img, unsigned char *img_point, QList<QByteArray>* transList, unsigned int *transCameraIndex,
                           unsigned short picStartX, unsigned short picStartY, unsigned short picWidth, unsigned short picHeight, unsigned int photoindex)
    :send_img_(img),
      send_img_point_(img_point),
      list(transList),
      cameraIndex(transCameraIndex),
      pic_start_X_(picStartX),
      pic_start_Y_(picStartY),
      pic_width_(picWidth),
      pic_height_(picHeight),
      photo_index_(photoindex)

{

}

bool CompressSend::compressSendImg()
{
//    qDebug() << "0000000000000000000000" << QDateTime::currentDateTime().toString("yyyyy-MM-dd hh:mm:ss.zzz");
    picIndex++;
    int size = 0;
    unsigned short all_lines_counts = 600;
    unsigned short all_column_counts = 800;
    unsigned short start_line = 0;
    unsigned short end_line = 0;
    if(IMAGE_WIDTCH != pic_width_ || IMAGE_HEIGHT != pic_height_){
//        qDebug() << "+++++++++++++++++++++" << QDateTime::currentDateTime().toString("yyyyy-MM-dd hh:mm:ss.zzz");
        QRect temp_rect = QRect(pic_start_X_, pic_start_Y_, pic_width_, pic_height_);
        send_img_ = send_img_.copy(temp_rect);
//        QPixmap tempImg;
//        tempImg = QPixmap::fromImage(send_img_);
//        tempImg = tempImg.scaled(800, 600,Qt::IgnoreAspectRatio, Qt::FastTransformation);
//        send_img_ = tempImg.toImage();
        const uchar* imageData = send_img_.bits();
        int bytesPerLine = send_img_.bytesPerLine();
//        QByteArray byteArray((char*)imageData, bytesPerLine * pic_height_);
        QByteArray byte_send;
        QByteArray a;
        for(int i = 1;i <= 600*800;i++){
            byte_send.append(*(imageData+i*2-1));
        }
//        qDebug() << "****************" << byte_send.size();
//        for(int i = 0 ; i < all_lines_counts; i++)
//        {
//            QByteArray byte = byteArray.mid(size, bytesPerLine);
//            QByteArray byte_send;
//            for(int j = 0; j < 800;j++){
//                byte_send.append(byte.mid(4*j,1));
//            }
//            size += bytesPerLine;
//            QByteArray a;
//            start_line = static_cast<unsigned short>(i)*1;
//            end_line = (1 + static_cast<unsigned short>(i))*1;
#ifdef BigAndSmall
            a.append(IntToArray(qFromBigEndian(photo_index_)));
            a.append(ShortToArray(qFromBigEndian(all_lines_counts)));
            a.append(ShortToArray(qFromBigEndian(start_line)));
            a.append(ShortToArray(qFromBigEndian(all_lines_counts)));
            a.append(ShortToArray(qFromBigEndian(all_column_counts)));
#else
            a.append(IntToArray(photo_index_));
            a.append(ShortToArray(all_lines_counts));
            a.append(ShortToArray(start_line));
            a.append(ShortToArray(all_lines_counts));
            a.append(ShortToArray(all_column_counts));
#endif
            a.append(byte_send);
            list->append(a);
//        }
    }else{
        // 计算每行数据的字节数
        int bytesPerLine = send_img_.bytesPerLine();
        // 创建一个 QByteArray 对象，并将图像数据复制到其中
//        QByteArray byteArray((char*)send_img_point_, bytesPerLine * IMAGE_HEIGHT);
        QByteArray byte_send;
        QByteArray a;
//        qDebug() << "11111111111111111111111111111" << QDateTime::currentDateTime().toString("yyyyy-MM-dd hh:mm:ss.zzz");

        for(int i = 0; i < 600; i++){
            unsigned int line_num = bytesPerLine*10*i;
            for(int j = 1; j <= 800; j++){
                byte_send.append(*(send_img_point_+j*20-1+line_num));
            }
        }
#ifdef BigAndSmall
        a.append(IntToArray(qFromBigEndian(photo_index_)));
        a.append(ShortToArray(qFromBigEndian(all_lines_counts)));
        a.append(ShortToArray(qFromBigEndian(start_line)));
        a.append(ShortToArray(qFromBigEndian(all_lines_counts)));
        a.append(ShortToArray(qFromBigEndian(all_column_counts)));
#else
        a.append(IntToArray(photo_index_));
        a.append(ShortToArray(all_lines_counts));
        a.append(ShortToArray(start_line));
        a.append(ShortToArray(all_lines_counts));
        a.append(ShortToArray(all_column_counts));
#endif
        a.append(byte_send);
        list->append(a);
    }
//    qDebug() << "888888888888888888" << QDateTime::currentDateTime().toString("yyyyy-MM-dd hh:mm:ss.zzz");
    //    emit CompressPictureOver();
    (*cameraIndex)++;
    return true;
}

QByteArray CompressSend::IntToArray(int v)
{
    int size = sizeof (int);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}

QByteArray CompressSend::ShortToArray(unsigned short v)
{
    int size = sizeof (unsigned short);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}
